package com.airconditioner.sales.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.airconditioner.sales.R;

import java.util.ArrayList;
import java.util.List;


public class HomeListAdapter extends RecyclerView.Adapter<HomeListAdapter.ItemViewHolder> {
    private LayoutInflater inflater;
    private MyItemClickListener itemClickListener;
    Context mContext;
    List<String> names = new ArrayList<>();
    List<Integer> reIds = new ArrayList<>();

    public HomeListAdapter(Context context, List<String> names, List<Integer> reIds) {
        mContext = context;
        this.names = names;
        this.reIds = reIds;
        inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public HomeListAdapter.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.activity_home_list_row, null);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(v, (Integer) v.getTag());
            }
        });
        ItemViewHolder viewHolder = new ItemViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        ItemViewHolder viewHolder = holder;
        viewHolder.itemView.setTag(position);
//        DisplayMetrics dm = new DisplayMetrics();
//        //获取屏幕信息
//        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
//        int screenWidth = dm.widthPixels;
//        int screenHeigh = dm.heightPixels;
//        RelativeLayout.LayoutParams linearParams = new RelativeLayout.LayoutParams(screenWidth / 2, screenHeigh / 3 - 100);
//        viewHolder.lin.setLayoutParams(linearParams);
        if (position < names.size()) {
            viewHolder.iconView.setImageResource(reIds.get(position));
            viewHolder.nameView.setText(names.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return names.size();
    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder {
        public ImageView iconView;
        public TextView nameView;
        private LinearLayout lin;

        public ItemViewHolder(View itemView) {
            super(itemView);
            iconView = (ImageView) itemView.findViewById(R.id.app_row_iv);
            nameView = (TextView) itemView.findViewById(R.id.app_row_tv);
            lin = (LinearLayout) itemView.findViewById(R.id.lin);
        }
    }

    public interface MyItemClickListener {
        void onItemClick(View view, int postion);
    }

    public void setOnItemClickListener(MyItemClickListener listener) {
        this.itemClickListener = listener;
    }
}
